package com.ecar.api.support.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ecar.common.util.JSONUtil;

public class TempHttpUtil {
	private static Logger log = LoggerFactory.getLogger(TempHttpUtil.class);
	
	public static int sendPost_JSON(String url, Map<String, Object> obj,String IPCType) {
        int  result = 0 ;
		BufferedReader in = null;
		OutputStream out = null;
		HttpURLConnection conn = null;
		try {
			String companyId=Props.getValue("ecar.companyId");
			String u="http://172.16.5.101:8080"+url+"?company="+companyId;
			URL realUrl = new URL(u);
			// 打开和URL之间的连接
			conn = (HttpURLConnection) realUrl.openConnection();
			// 设置允许输出
			conn.setDoOutput(true);
			conn.setDoInput(true);
			
			conn.setRequestMethod("POST");
			conn.setRequestProperty("accept", "*/*");
			conn.setRequestProperty("connection", "Keep-Alive");
			conn.setRequestProperty("Content-Type", "application/json");
			
			conn.connect();
			// 获取URLConnection对象对应的输出流
			out = conn.getOutputStream();
			JSONObject map2=new JSONObject();
			String source=Props.getValue("ecar.source");
			map2.put("Source", source);
			map2.put("CompanyId", companyId);
			map2.put("IPCType", IPCType);
			map2.put(IPCType, obj);
			// 发送请求参数
			log.info(map2.toString());
			out.write((map2.toString()).getBytes());
			// flush输出流的缓冲
			out.flush();
			result = conn.getResponseCode();
			log.info("result=============="+conn.getResponseMessage());
			log.info("conn=============="+conn.toString());
		} catch (Exception e) {
			System.out.println("发送 POST 请求出现异常！" + e);
			e.printStackTrace();
		}
		// 使用finally块来关闭输出流、输入流
		finally {
			try {
				if (out != null) {
					out.close();
				}
				if (in != null) {
					in.close();
				}

			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return result;
	}
	public static int sendPost_JSON(String url, JSONArray obj,String IPCType) {
        int  result = 0 ;
		BufferedReader in = null;
		OutputStream out = null;
		HttpURLConnection conn = null;
		try {
			String companyId=Props.getValue("ecar.companyId");
			String u="http://172.16.5.101:8080"+url+"?company="+companyId;
			URL realUrl = new URL(u);
			// 打开和URL之间的连接
			conn = (HttpURLConnection) realUrl.openConnection();
			// 设置允许输出
			conn.setDoOutput(true);
			conn.setDoInput(true);
			
			conn.setRequestMethod("POST");
			conn.setRequestProperty("accept", "*/*");
			conn.setRequestProperty("connection", "Keep-Alive");
			conn.setRequestProperty("Content-Type", "application/json");
			
			conn.connect();
			// 获取URLConnection对象对应的输出流
			out = conn.getOutputStream();
			JSONObject map2=new JSONObject();
			String source=Props.getValue("ecar.source");
			map2.put("Source", source);
			map2.put("CompanyId", companyId);
			map2.put("IPCType", IPCType);
			map2.put(IPCType, obj);
			log.info(map2.toString());
			// 发送请求参数
			out.write((map2.toString()).getBytes());
			// flush输出流的缓冲
			out.flush();
			result = conn.getResponseCode();
			log.info("result=============="+conn.getResponseMessage());
			log.info("conn=============="+conn.toString());
		} catch (Exception e) {
			System.out.println("发送 POST 请求出现异常！" + e);
			e.printStackTrace();
		}
		// 使用finally块来关闭输出流、输入流
		finally {
			try {
				if (out != null) {
					out.close();
				}
				if (in != null) {
					in.close();
				}

			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return result;
	}
}
